local session = require ('resty.session').new({store='shm',cookie={renew=1200}})
local sys_statck=require('vanilla.v.ctxstatck'):sys_statck()

local user = require 'library.user'
local helper = require 'library.helpers'

local sessionPlugin = require('vanilla.v.plugin'):new()

function sessionPlugin:routerStartup(request, response)
    --print_r('<pre>')
    if request.method == 'GET' then
        --print_r('-----------' .. sprint_r(request.headers) .. '----------')
    else
        --print_r(request.headers)
    end
end

function sessionPlugin:routerShutdown(request, response)
end

function sessionPlugin:dispatchLoopStartup(request, response)
end

function sessionPlugin:preDispatch(request, response)

end

function sessionPlugin:afterControllerInit(request, response,controller)
	local dispatcher=sys_statck:get()
	session.start()
	dispatcher.session=session;

	local startnum,endnum = string.find(dispatcher.request.controller_name,'admin');
	if startnum==1 and 'admin.index'~= request.controller_name and not user:is_login()  then
		controller:redirect(helper:url('admin/index/login'));
		--ngx.say( dispatcher.request.controller_name..'|access denied')
		ngx.eof()
	end
end

function sessionPlugin:postDispatch(request, response)
end

function sessionPlugin:dispatchLoopShutdown(request, response)
	--session:save()
end

return sessionPlugin
